//
//  ViewController.swift
//  JavascriptCoreLearning
//
//  Created by Linda Zheng on 2017/11/2.
//  Copyright © 2017年 Linda Zheng. All rights reserved.
//

import UIKit
import JavaScriptCore
import WebKit

@objc protocol TestJSProtocol: JSExport {
    var name: String { get set }
    var age: Int { get set }
    static func create(name: String, age: Int) -> User
}

class User: NSObject, TestJSProtocol {
    
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age  = age
    }
    
    static func create(name: String, age: Int) -> User {
        return User(name: name, age: age)
    }
}

class ViewController: UIViewController {

    let web = UIWebView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        nativeCallJavascriptWithWebView()
//        nativeCallJavascriptWithJavascriptCore()
//        nativeBridgeToJavascriptWithJSExport()

    }
    
    func nativeCallJavascriptWithJavascriptCore() {
        guard let context = JSContext() else { return }
        context.evaluateScript("var num = 3;")
        context.evaluateScript("var toAdd = 2;")
        if let value = context.evaluateScript("num + toAdd") {
            print(value)
            print("hello ...")
        }
    }
    
    func nativeCallJavascriptWithWebView() {
        
        web.delegate = self
        
        web.stringByEvaluatingJavaScript(from: "var num = 3;")
        web.stringByEvaluatingJavaScript(from: "var toAdd = 1;")
        
        let request = URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "index", ofType: "html")!))
        web.loadRequest(request)
        
        guard let context = web.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as? JSContext else { return }
        context.setObject(User.self, forKeyedSubscript: "User" as (NSCopying & NSObjectProtocol)!)
        context.evaluateScript("var user = User.createWithNameAge('linda', 25)")
        if let name = context.evaluateScript("user.name") {
            print(name)
        }
        if let age = context.evaluateScript("user.age") {
            print("age=", age)
        }
        
        if let value = web.stringByEvaluatingJavaScript(from: "num + toAdd") {
            print(value, "web")
        }
    }
    
    func nativeBridgeToJavascriptWithJSExport() {
        guard let context = JSContext() else { return }
        
        context.setObject(User.self, forKeyedSubscript: "User" as (NSCopying & NSObjectProtocol)!)
        context.evaluateScript("var user = User.createWithNameAge('linda', 25)")
        if let name = context.evaluateScript("user.name") {
            print(name)
        }
        if let age = context.evaluateScript("user.age") {
            print("age=", age)
        }
    }

}

extension ViewController: UIWebViewDelegate {
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        guard let context = webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as? JSContext else {
            return true
        }
        print("shouldStartLoadWith")
        return true
    }
    
    func webViewDidStartLoad(_ webView: UIWebView) {
        guard let context = webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as? JSContext else {
            return
        }
        print("webViewDidStartLoad")
    }
    func webViewDidFinishLoad(_ webView: UIWebView) {
        guard let context = webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as? JSContext else {
            return
        }
        print("webViewDidFinishLoad")
    }
}

